<?php # Timer initialization.
$time_start = microtime_float();
require_once ('config.php');
$config = simplexml_load_file('config.xml');
require_once ('header.php');
require_once ('posts.php');
require_once ('footer.php');
do_header($config);
$mysqli=new mysqli($dbserver,$dbuser,$dbpassword,$dbdatabase);
if (isset($_POST['content'])) {
    try {
        $content = htmlentities($_POST['content']);
        $stmt = $mysqli->prepare("INSERT INTO posts (`content`,`post_date`,`author_IP`) VALUES(?,FROM_UNIXTIME(?),?)");
        $stmt->bind_param('sis',$content,time(),$_SERVER['REMOTE_ADDR']);
        $stmt->execute();
    }
    catch (Exception $e){
        echo 'Something went wrong with your post attempt.';
    }
}
if (isset($_POST['comment'])) {
    try{
        $comment = htmlentities($_POST['comment']);
        $postID = $_POST['post'];
        $stmt = $mysqli->prepare("INSERT INTO `comments` (`commentText`,`postID`,`comment_date`,`author_IP`) VALUES(?,?,FROM_UNIXTIME(?),?)");
        $stmt->bind_param('siis',$comment,$postID,time(),$_SERVER['REMOTE_ADDR']);
        $stmt->execute();
        $stmt = $mysqli->prepare("UPDATE `posts` SET `comment_count`=`comment_count`+1 WHERE `ID`=?");
        $stmt->bind_param('i',$postID);
        $stmt->execute();
   }
   catch (Exception $e){
       echo 'Something went wrong with your post attempt.';
   }
}
if (isset($_GET['post'])){
    displaypost($mysqli,$_GET['post']);
} else {
    displayallposts($mysqli);
}
    require_once ('leftcol.php');
    require_once ('rightcol.php');
    do_footer($config, $mysqli, $time_start);
    
function microtime_float() {
   list($usec, $sec) = explode(" ", microtime());
   return ((float)$usec + (float)$sec);
}
?>
